package com.bifang.core.oplog.aspect;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.bifang.common.constant.enums.common.OpTypeEnum;
import com.bifang.common.exception.AppException;
import com.bifang.common.helper.ApplicationContextHelper;
import com.bifang.common.util.OplogUtil;
import com.bifang.common.vo.TableChangeLogVO;
import com.bifang.core.oplog.integration.OplogIntegration;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Aspect
@Component
@Order(4)
public class OplogTableAspect {
    @Autowired OplogIntegration oplogIntegration;

    @Value("${system.table.not.save.tableChange}")
    private String notSaveTableChangeList;

    private static final Logger logger = LoggerFactory.getLogger(OplogTableAspect.class);

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.save(..))")
    public Object afterInsert(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        Object obj = joinPoint.proceed();
        if (oplogId == null) {
            return obj;
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            Class<?> cls = argument.getClass();
            TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);

            Assert.notNull(
                    tableInfo,
                    "error: can not execute. because can not find cache of TableInfo for entity!",
                    new Object[0]);
            if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                // 工作流相关表不处理
                return obj;
            }
            if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                // 工作流相关表不处理
                return obj;
            }
            List<String> list =
                    Arrays.stream(notSaveTableChangeList.split(","))
                            .map(s -> s)
                            .collect(Collectors.toList());
            if (list.contains(tableInfo.getTableName())) {
                return obj;
            }

            String keyProperty = tableInfo.getKeyProperty();
            Assert.notEmpty(
                    keyProperty,
                    "error: can not execute. because can not find column for id from entity!",
                    new Object[0]);
            Object idVal = ReflectionKit.getFieldValue(argument, keyProperty);
            Class<?> mapClass = joinPoint.getTarget().getClass();
            TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
            tableChangeLogVO.setNewObj(argument);
            tableChangeLogVO.setMaperClass(mapClass);
            tableChangeLogVO.setDataField(keyProperty);
            tableChangeLogVO.setClassType(cls);
            tableChangeLogVO.setOpType(OpTypeEnum.ADD.getType());
            tableChangeLogVO.setOpTable(tableInfo.getTableName());
            if (idVal != null) {
                tableChangeLogVO.setUniqueValue(idVal.toString());
            }
            tableChangeLogVO.setOplogId(oplogId);
            oplogIntegration.saveChangeLog(tableChangeLogVO);
            return obj;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.saveBatch(..))")
    public Object afterSaveBatch(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        Object obj = joinPoint.proceed();
        if (oplogId == null) {
            return obj;
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            if (argument == null || ((List) argument).size() <= 0) {
                return obj;
            }
            Class<?> cls = null;
            for (Object object : (List) argument) {
                cls = object.getClass();
                break;
            }

            TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
            Assert.notNull(
                    tableInfo,
                    "error: can not execute. because can not find cache of TableInfo for entity!",
                    new Object[0]);
            if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                // 工作流相关表不处理
                return obj;
            }
            if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                // 工作流相关表不处理
                return obj;
            }
            List<String> list =
                    Arrays.stream(notSaveTableChangeList.split(","))
                            .map(s -> s)
                            .collect(Collectors.toList());
            if (list.contains(tableInfo.getTableName())) {
                return obj;
            }
            String keyProperty = tableInfo.getKeyProperty();
            Assert.notEmpty(
                    keyProperty,
                    "error: can not execute. because can not find column for id from entity!",
                    new Object[0]);
            Class<?> mapClass = joinPoint.getTarget().getClass();
            TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
            tableChangeLogVO.setNewObj(argument);
            tableChangeLogVO.setMaperClass(mapClass);
            tableChangeLogVO.setDataField(keyProperty);
            tableChangeLogVO.setClassType(cls);
            tableChangeLogVO.setOpType(OpTypeEnum.ADD.getType());
            tableChangeLogVO.setOpTable(tableInfo.getTableName());
            tableChangeLogVO.setUniqueValue(null);
            tableChangeLogVO.setOplogId(oplogId);
            oplogIntegration.saveChangeLog(tableChangeLogVO);
            return obj;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.updateById(..))")
    public Object beforeUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        if (oplogId == null) {
            return joinPoint.proceed();
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            Class<?> cls = argument.getClass();
            TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
            Assert.notNull(
                    tableInfo,
                    "error: can not execute. because can not find cache of TableInfo for entity!",
                    new Object[0]);
            if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                // 工作流相关表不处理
                return joinPoint.proceed();
            }
            if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                // 工作流相关表不处理
                return joinPoint.proceed();
            }
            List<String> list =
                    Arrays.stream(notSaveTableChangeList.split(","))
                            .map(s -> s)
                            .collect(Collectors.toList());
            if (list.contains(tableInfo.getTableName())) {
                return joinPoint.proceed();
            }
            String keyProperty = tableInfo.getKeyProperty();
            Assert.notEmpty(
                    keyProperty,
                    "error: can not execute. because can not find column for id from entity!",
                    new Object[0]);
            Object idVal = ReflectionKit.getFieldValue(argument, keyProperty);
            try {
                Class clazz = joinPoint.getTarget().getClass();
                // Object bean = ApplicationContextHelper.getBean("sysUserServiceImpl");
                Method method = ApplicationContextHelper.getMethod(clazz, "getById");
                Object oldObj = method.invoke(joinPoint.getTarget(), (Serializable) idVal);
                if (oldObj == null) {
                    throw new AppException("更新的数据不存在");
                }
                TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
                tableChangeLogVO.setNewObj(argument);
                tableChangeLogVO.setMaperClass(clazz);
                tableChangeLogVO.setDataField(keyProperty);
                tableChangeLogVO.setClassType(cls);
                tableChangeLogVO.setOpType(OpTypeEnum.EDIT.getType());
                tableChangeLogVO.setOpTable(tableInfo.getTableName());
                if (idVal != null) {
                    tableChangeLogVO.setUniqueValue(idVal.toString());
                }
                tableChangeLogVO.setOplogId(oplogId);
                tableChangeLogVO.setOldObj(oldObj);
                oplogIntegration.saveChangeLog(tableChangeLogVO);

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            Object obj = joinPoint.proceed();
            return obj;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.updateBatchById(..))")
    public Object beforeUpdateBatchById(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        if (oplogId == null) {
            return joinPoint.proceed();
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            Class<?> cls = null;
            String keyProperty = null;
            TableInfo tableInfo = null;
            List<Serializable> ids = new ArrayList<>();
            for (Object object : (List) argument) {
                cls = object.getClass();
                tableInfo = TableInfoHelper.getTableInfo(cls);
                Assert.notNull(
                        tableInfo,
                        "error: can not execute. because can not find cache of TableInfo for entity!",
                        new Object[0]);
                keyProperty = tableInfo.getKeyProperty();
                Assert.notEmpty(
                        keyProperty,
                        "error: can not execute. because can not find column for id from entity!",
                        new Object[0]);
                Object idVal = ReflectionKit.getFieldValue(object, keyProperty);
                ids.add((Serializable) idVal);
            }
            if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                // 工作流相关表不处理
                return joinPoint.proceed();
            }
            if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                // 工作流相关表不处理
                return joinPoint.proceed();
            }
            List<String> list =
                    Arrays.stream(notSaveTableChangeList.split(","))
                            .map(s -> s)
                            .collect(Collectors.toList());
            if (list.contains(tableInfo.getTableName())) {
                return joinPoint.proceed();
            }
            try {
                Class clazz = joinPoint.getTarget().getClass();
                // Object bean = ApplicationContextHelper.getBean("sysUserServiceImpl");
                Method method = ApplicationContextHelper.getMethod(clazz, "listByIds");
                Object oldObj = method.invoke(joinPoint.getTarget(), (Serializable) ids);
                TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
                tableChangeLogVO.setNewObj(argument);
                tableChangeLogVO.setMaperClass(clazz);
                tableChangeLogVO.setDataField(keyProperty);
                tableChangeLogVO.setClassType(cls);
                tableChangeLogVO.setOpType(OpTypeEnum.EDIT.getType());
                tableChangeLogVO.setOpTable(tableInfo.getTableName());
                tableChangeLogVO.setUniqueValue(null);
                tableChangeLogVO.setOplogId(oplogId);
                tableChangeLogVO.setOldObj(oldObj);
                oplogIntegration.saveChangeLog(tableChangeLogVO);

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            Object obj = joinPoint.proceed();
            return obj;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.removeById(..))")
    public Object beforeDelete(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        if (oplogId == null) {
            return joinPoint.proceed();
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            Class clazz = joinPoint.getTarget().getClass();
            Method method = ApplicationContextHelper.getMethod(clazz, "getById");
            try {
                Object oldObj = method.invoke(joinPoint.getTarget(), (Serializable) argument);
                if (oldObj == null) {
                    return joinPoint.proceed();
                }
                Class<?> cls = oldObj.getClass();
                TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);

                Assert.notNull(
                        tableInfo,
                        "error: can not execute. because can not find cache of TableInfo for entity!",
                        new Object[0]);
                if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                    // 工作流相关表不处理
                    return joinPoint.proceed();
                }
                if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                    // 工作流相关表不处理
                    return joinPoint.proceed();
                }
                List<String> list =
                        Arrays.stream(notSaveTableChangeList.split(","))
                                .map(s -> s)
                                .collect(Collectors.toList());
                if (list.contains(tableInfo.getTableName())) {
                    return joinPoint.proceed();
                }
                String keyProperty = tableInfo.getKeyProperty();
                Assert.notEmpty(
                        keyProperty,
                        "error: can not execute. because can not find column for id from entity!",
                        new Object[0]);
                Object idVal = argument;
                TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
                tableChangeLogVO.setOldObj(oldObj);
                tableChangeLogVO.setMaperClass(clazz);
                tableChangeLogVO.setDataField(keyProperty);
                tableChangeLogVO.setClassType(cls);
                tableChangeLogVO.setOpType(OpTypeEnum.DELETE.getType());
                Field[] fields = cls.getDeclaredFields();
                for (Field field : fields) {
                    if (field.getName().equals("del")) {
                        tableChangeLogVO.setOpType(OpTypeEnum.DEL.getType());
                        break;
                    }
                }

                tableChangeLogVO.setOpTable(tableInfo.getTableName());
                tableChangeLogVO.setUniqueValue(idVal.toString());
                tableChangeLogVO.setOplogId(oplogId);
                oplogIntegration.saveChangeLog(tableChangeLogVO);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            Object obj = joinPoint.proceed();
            return obj;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.removeByIds(..))")
    public Object beforeDeleteByIds(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        if (oplogId == null) {
            return joinPoint.proceed();
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            Class clazz = joinPoint.getTarget().getClass();
            Method method = ApplicationContextHelper.getMethod(clazz, "listByIds");
            try {
                Object oldObj = method.invoke(joinPoint.getTarget(), argument);
                Class<?> cls = null;
                for (Object obj : (List) oldObj) {
                    cls = obj.getClass();
                    break;
                }
                if (cls == null) {
                    return joinPoint.proceed();
                }
                TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
                Assert.notNull(
                        tableInfo,
                        "error: can not execute. because can not find cache of TableInfo for entity!",
                        new Object[0]);
                if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                    // 工作流相关表不处理
                    return joinPoint.proceed();
                }
                if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                    // 工作流相关表不处理
                    return joinPoint.proceed();
                }
                List<String> list =
                        Arrays.stream(notSaveTableChangeList.split(","))
                                .map(s -> s)
                                .collect(Collectors.toList());
                if (list.contains(tableInfo.getTableName())) {
                    return joinPoint.proceed();
                }
                String keyProperty = tableInfo.getKeyProperty();
                Assert.notEmpty(
                        keyProperty,
                        "error: can not execute. because can not find column for id from entity!",
                        new Object[0]);
                TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
                tableChangeLogVO.setOldObj(oldObj);
                tableChangeLogVO.setMaperClass(clazz);
                tableChangeLogVO.setDataField(keyProperty);
                tableChangeLogVO.setClassType(cls);
                tableChangeLogVO.setOpType(OpTypeEnum.DELETE.getType());
                Field[] fields = cls.getDeclaredFields();
                for (Field field : fields) {
                    if (field.getName().equals("del")) {
                        tableChangeLogVO.setOpType(OpTypeEnum.DEL.getType());
                        break;
                    }
                }

                tableChangeLogVO.setOpTable(tableInfo.getTableName());
                tableChangeLogVO.setUniqueValue(null);
                tableChangeLogVO.setOplogId(oplogId);
                oplogIntegration.saveChangeLog(tableChangeLogVO);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            Object obj = joinPoint.proceed();
            return obj;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.saveOrUpdate(..))")
    public Object aroundSaveOrUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        if (oplogId == null) {
            return joinPoint.proceed();
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            if (args.length > 1) {
                return joinPoint.proceed();
            }
            Object argument = args[0];
            Class<?> cls = argument.getClass();
            TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
            Assert.notNull(
                    tableInfo,
                    "error: can not execute. because can not find cache of TableInfo for entity!",
                    new Object[0]);
            if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                // 工作流相关表不处理
                return joinPoint.proceed();
            }
            if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                // 工作流相关表不处理
                return joinPoint.proceed();
            }
            List<String> list =
                    Arrays.stream(notSaveTableChangeList.split(","))
                            .map(s -> s)
                            .collect(Collectors.toList());
            if (list.contains(tableInfo.getTableName())) {
                return joinPoint.proceed();
            }
            String keyProperty = tableInfo.getKeyProperty();
            Assert.notEmpty(
                    keyProperty,
                    "error: can not execute. because can not find column for id from entity!",
                    new Object[0]);
            Object idVal = ReflectionKit.getFieldValue(argument, keyProperty);
            Class<?> mapClass = joinPoint.getTarget().getClass();
            Method method = ApplicationContextHelper.getMethod(mapClass, "getById");
            TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
            tableChangeLogVO.setOpType(OpTypeEnum.ADD.getType());
            try {
                if (idVal != null) {
                    Object oldObj = method.invoke(joinPoint.getTarget(), (Serializable) idVal);
                    if (oldObj != null) {
                        tableChangeLogVO.setOldObj(oldObj);
                        tableChangeLogVO.setOpType(OpTypeEnum.EDIT.getType());
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            Object ret = joinPoint.proceed();
            Object[] argsNew = joinPoint.getArgs();
            Object argumentNew = argsNew[0];
            Object idValNew = ReflectionKit.getFieldValue(argumentNew, keyProperty);
            if (idValNew == null) {
                throw new AppException("新增数据未获取到主键信息");
            }
            tableChangeLogVO.setNewObj(argumentNew);
            tableChangeLogVO.setMaperClass(mapClass);
            tableChangeLogVO.setDataField(keyProperty);
            tableChangeLogVO.setClassType(cls);
            tableChangeLogVO.setOpTable(tableInfo.getTableName());
            tableChangeLogVO.setUniqueValue(idValNew.toString());
            tableChangeLogVO.setOplogId(oplogId);
            oplogIntegration.saveChangeLog(tableChangeLogVO);
            return ret;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }

    @Around("execution(* com.baomidou.mybatisplus.extension.service.IService.saveOrUpdateBatch(*))")
    public Object aroundSaveOrUpdateBatch(ProceedingJoinPoint joinPoint) throws Throwable {
        Long oplogId = OplogUtil.getOplogId();
        if (oplogId == null) {
            Object obj = joinPoint.proceed();
            return obj;
        }
        logger.info("触发了表变动方法：" + oplogId);
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            List<TableChangeLogVO> editChangList = new ArrayList<>();
            boolean flag = true;
            Map<String, String> editMap = new HashMap<>();
            for (Object object : (List) argument) {
                Class<?> cls = object.getClass();
                TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
                Assert.notNull(
                        tableInfo,
                        "error: can not execute. because can not find cache of TableInfo for entity!",
                        new Object[0]);
                if (tableInfo.getTableName().toUpperCase().startsWith("ACT_")) {
                    // 工作流相关表不处理
                    flag = false;
                    break;
                }
                if (tableInfo.getTableName().toUpperCase().startsWith("FLW_")) {
                    // 工作流相关表不处理
                    flag = false;
                    break;
                }
                List<String> list =
                        Arrays.stream(notSaveTableChangeList.split(","))
                                .map(s -> s)
                                .collect(Collectors.toList());
                if (list.contains(tableInfo.getTableName())) {
                    flag = false;
                    break;
                }
                String keyProperty = tableInfo.getKeyProperty();
                Assert.notEmpty(
                        keyProperty,
                        "error: can not execute. because can not find column for id from entity!",
                        new Object[0]);
                Object idVal = ReflectionKit.getFieldValue(object, keyProperty);
                Class<?> mapClass = joinPoint.getTarget().getClass();
                Method method = ApplicationContextHelper.getMethod(mapClass, "getById");

                try {
                    if (idVal != null) {
                        Object oldObj = method.invoke(joinPoint.getTarget(), (Serializable) idVal);
                        if (oldObj != null) {
                            TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
                            tableChangeLogVO.setOldObj(oldObj);
                            tableChangeLogVO.setOpType(OpTypeEnum.EDIT.getType());
                            tableChangeLogVO.setNewObj(object);
                            tableChangeLogVO.setMaperClass(mapClass);
                            tableChangeLogVO.setDataField(keyProperty);
                            tableChangeLogVO.setClassType(cls);
                            tableChangeLogVO.setOpTable(tableInfo.getTableName());
                            tableChangeLogVO.setUniqueValue(idVal.toString());
                            tableChangeLogVO.setOplogId(oplogId);
                            editChangList.add(tableChangeLogVO);
                            editMap.put(idVal.toString(), idVal.toString());
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            Object obj = joinPoint.proceed();
            if (flag) {
                for (Object object : (List) argument) {
                    Class<?> cls = object.getClass();
                    TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
                    Assert.notNull(
                            tableInfo,
                            "error: can not execute. because can not find cache of TableInfo for entity!",
                            new Object[0]);
                    String keyProperty = tableInfo.getKeyProperty();
                    Assert.notEmpty(
                            keyProperty,
                            "error: can not execute. because can not find column for id from entity!",
                            new Object[0]);
                    Object idVal = ReflectionKit.getFieldValue(object, keyProperty);
                    Class<?> mapClass = joinPoint.getTarget().getClass();
                    if (idVal == null) {
                        throw new AppException("新增后唯一id未获取到");
                    }
                    if (!editMap.containsKey(idVal.toString())) {
                        TableChangeLogVO tableChangeLogVO = new TableChangeLogVO();
                        tableChangeLogVO.setOpType(OpTypeEnum.ADD.getType());
                        tableChangeLogVO.setNewObj(object);
                        tableChangeLogVO.setMaperClass(mapClass);
                        tableChangeLogVO.setDataField(keyProperty);
                        tableChangeLogVO.setClassType(cls);
                        tableChangeLogVO.setOpTable(tableInfo.getTableName());
                        tableChangeLogVO.setUniqueValue(idVal.toString());
                        tableChangeLogVO.setOplogId(oplogId);
                        editChangList.add(tableChangeLogVO);
                    }
                }
                if (!CollectionUtils.isEmpty(editChangList)) {
                    for (TableChangeLogVO tableChangeLogVO : editChangList) {
                        oplogIntegration.saveChangeLog(tableChangeLogVO);
                    }
                }
            }

            return obj;
        } else {
            throw new AppException("操作数据库时未传入参数");
        }
    }
}
